package com.ruoyi.archives.controller;

import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.archives.domain.SysArchivessystem;
import com.ruoyi.archives.mapper.SysArchivessystemMapper;
import com.ruoyi.archives.utile.DirectorRoleUtile;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UuidUtils;
import com.ruoyi.system.domain.SysNotice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.archives.domain.SysStudywork;
import com.ruoyi.archives.service.ISysStudyworkService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学习/工作简历Controller
 * 
 * @author gzhankh
 * @date 2022-02-26
 */
@Api("学生学习/工作简历接口")
@RestController
@RequestMapping("/archives/studywork")
public class SysStudyworkController extends BaseController
{
    @Autowired
    private ISysStudyworkService sysStudyworkService;

    @Autowired
    private SysArchivessystemMapper sysArchivessystemMapper;

    @Autowired
    private DirectorRoleUtile directorRoleUtile;

    /**
     * 查询学习/工作简历列表
     */
    @ApiOperation("查询学习/工作简历列表")
    @PreAuthorize("@ss.hasPermi('archives:studywork:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysStudywork sysStudywork)
    {
        startPage();
        List<SysStudywork> list = sysStudyworkService.selectSysStudyworkList(sysStudywork);
        List<SysStudywork> studyworkList = list.stream().sorted(Comparator.comparing(SysStudywork::getStartTime).reversed()).collect(Collectors.toList());
        LoginUser loginUser = SecurityUtils.getLoginUser();

        int stDirectorRole = directorRoleUtile.getSTDirectorRole(loginUser);

        if(!SecurityUtils.isAdmin(loginUser.getUserId())&&stDirectorRole==1){
            ArrayList<SysStudywork> sysStudyworks = new ArrayList<>();
            for (SysStudywork studywork : list) {
                if(studywork.getArcNum().equals(loginUser.getUserId().toString())){
                    sysStudyworks.add(studywork);
                }
            }
            sysStudyworks.stream().sorted(Comparator.comparing(SysStudywork::getStartTime).reversed()).collect(Collectors.toList());
            return getDataTable(sysStudyworks);
        }else {
            return getDataTable(studyworkList);
        }
    }

    /**
     * 导出学习/工作简历列表
     */
    @ApiOperation("导出学习/工作简历列表")
    @PreAuthorize("@ss.hasPermi('archives:studywork:export')")
    @Log(title = "学习/工作简历", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysStudywork sysStudywork)
    {
        List<SysStudywork> list = sysStudyworkService.selectSysStudyworkList(sysStudywork);
        ExcelUtil<SysStudywork> util = new ExcelUtil<SysStudywork>(SysStudywork.class);
        util.exportExcel(response, list, "学习/工作简历数据");
    }

    /**
     * 获取学习/工作简历详细信息
     */
    @ApiOperation("获取学习/工作简历详细信息")
    @PreAuthorize("@ss.hasPermi('archives:studywork:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(sysStudyworkService.selectSysStudyworkById(id));
    }

    /**
     * 新增学习/工作简历
     */
    @ApiOperation("新增学习/工作简历")
    @PreAuthorize("@ss.hasPermi('archives:studywork:add')")
    @Log(title = "学习/工作简历", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysStudywork sysStudywork)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        sysStudywork.setId(UuidUtils.get8UUID());
        sysStudywork.setUserName(loginUser.getUsername());
        sysStudywork.setArcNum(loginUser.getUserId().toString());
        return toAjax(sysStudyworkService.insertSysStudywork(sysStudywork));
    }

    /**
     * 修改学习/工作简历
     */
    @ApiOperation("修改学习/工作简历")
    @PreAuthorize("@ss.hasPermi('archives:studywork:edit')")
    @Log(title = "学习/工作简历", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysStudywork sysStudywork)
    {
        return toAjax(sysStudyworkService.updateSysStudywork(sysStudywork));
    }

    /**
     * 删除学习/工作简历
     */
    @ApiOperation("删除学习/工作简历")
    @PreAuthorize("@ss.hasPermi('archives:studywork:remove')")
    @Log(title = "学习/工作简历", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sysStudyworkService.deleteSysStudyworkByIds(ids));
    }
}
